package com.mall.common.security;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.common.CommonSetting;
import com.mall.common.domain.manage.Role;
import com.mall.common.domain.manage.S_userDomain;
import com.mall.common.service.S_userService;
import com.mall.common.util.CommonSettingUtils;
import com.mall.common.util.PropertiesHelper;

/**
 * Service实现类 - 后台权限认证
 */

@Service
@Transactional(readOnly=true)
public class AdminDetailsServiceImpl implements UserDetailsService {

	@Resource private S_userService s_userService;

	
	// 获得管理角色数组
	@Transactional(readOnly=true)
	public Collection<GrantedAuthority> getGrantedAuthorities(S_userDomain admin) {
		Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		String system_manager_default_role = PropertiesHelper.getPropertiesValue("config", "system_manager_default_role");
		grantedAuthorities.add(new GrantedAuthorityImpl(system_manager_default_role));
		//如果是系统管理员，则只需要增加系统管理员的角色
		if(admin.isSystemManager()){
			//增加系统管理员的权限
			String system_manager_role = PropertiesHelper.getPropertiesValue("config", "system_manager_role");
			grantedAuthorities.add(new GrantedAuthorityImpl(system_manager_role));
			return grantedAuthorities;
		}
		
		for (Role role : admin.getRoleSet()) {
			grantedAuthorities.add(new GrantedAuthorityImpl(role.getValue()));
		}
		return grantedAuthorities;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		S_userDomain admin = s_userService.getAdminByUsername(username);
		if (admin == null) {
			throw new UsernameNotFoundException("管理员[" + username + "]不存在!");
		}
		// 解除管理员账户锁定
		CommonSetting systemConfig = CommonSettingUtils.get();
		if (admin.getIsAccountLocked() == true) {
			if (systemConfig.getIsAdminLoginFailureLock() == true) {
				int loginFailureLockTime = systemConfig.getAdminLoginFailureLockTime();
				if (loginFailureLockTime != 0) {
					Date lockedDate = admin.getLockedDate();
					Date nonLockedTime = DateUtils.addMinutes(lockedDate, loginFailureLockTime);
					Date now = new Date();
					if (now.after(nonLockedTime)) {
						admin.setLoginFailureCount(0);
						admin.setIsAccountLocked(false);
						admin.setLockedDate(null);
						s_userService.update(admin);
					}
				}
			} else {
				admin.setLoginFailureCount(0);
				admin.setIsAccountLocked(false);
				admin.setLockedDate(null);
				s_userService.update(admin);
			}
		}
		admin.setAuthorities(getGrantedAuthorities(admin));
		return admin.getUser();
	}

}